<template>
  <span>{{ displayValue }}</span>
</template>

<script>
export default {
  name: 'CountTo',
  props: {
    startVal: {
      type: [Number, String],
      default: 0
    },
    endVal: {
      type: [Number, String],
      default: 0
    },
    duration: {
      type: Number,
      default: 1000
    },
    autoplay: {
      type: Boolean,
      default: true
    },
    decimals: {
      type: Number,
      default: 0
    },
    decimal: {
      type: String,
      default: '.'
    },
    separator: {
      type: String,
      default: ','
    },
    prefix: {
      type: String,
      default: ''
    },
    suffix: {
      type: String,
      default: ''
    },
    useEasing: {
      type: Boolean,
      default: true
    },
    easingFn: {
      type: Function,
      default: (t, b, c, d) => {
        return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b
      }
    }
  },
  data() {
    return {
      displayValue: this.formatValue(this.startVal),
      localStartVal: this.parseValue(this.startVal),
      printVal: null,
      paused: false,
      localDuration: this.duration,
      startTime: null,
      timestamp: null,
      remaining: null,
      rAF: null,
      lastValue: this.parseValue(this.startVal)
    }
  },
  computed: {
    parsedEndVal() {
      return this.parseValue(this.endVal)
    }
  },
  watch: {
    startVal() {
      this.localStartVal = this.parseValue(this.startVal)
      this.startAnimation()
    },
    endVal() {
      this.startAnimation()
    }
  },
  mounted() {
    if (this.autoplay) {
      this.startAnimation()
    }
    this.$emit('mounted')
  },
  beforeDestroy() {
    this.clearAnimationFrame()
  },
  methods: {
    parseValue(value) {
      if (value === null || value === undefined) {
        return 0
      }
      // 确保输入的是一个数字（如果是字符串，则转为数字）
      return typeof value === 'string' ? parseFloat(value) : Number(value)
    },
    formatValue(value) {
      const { decimals, decimal, separator, prefix, suffix } = this
      value = Number(value).toFixed(decimals)
      value += ''
      
      const x = value.split('.')
      let x1 = x[0]
      const x2 = x.length > 1 ? decimal + x[1] : ''
      
      const rgx = /(\d+)(\d{3})/
      if (separator && !isNaN(parseInt(x1, 10))) {
        while (rgx.test(x1)) {
          x1 = x1.replace(rgx, '$1' + separator + '$2')
        }
      }
      return prefix + x1 + x2 + suffix
    },
    
    clearAnimationFrame() {
      if (this.rAF) {
        cancelAnimationFrame(this.rAF)
      }
    },
    
    count(timestamp) {
      if (!this.startTime) this.startTime = timestamp
      this.timestamp = timestamp
      const progress = timestamp - this.startTime
      this.remaining = this.localDuration - progress
      
      if (this.useEasing) {
        if (this.localStartVal > this.parsedEndVal) {
          this.printVal = this.localStartVal - this.easingFn(progress, 0, this.localStartVal - this.parsedEndVal, this.localDuration)
        } else {
          this.printVal = this.easingFn(progress, this.localStartVal, this.parsedEndVal - this.localStartVal, this.localDuration)
        }
      } else {
        if (this.localStartVal > this.parsedEndVal) {
          this.printVal = this.localStartVal - ((this.localStartVal - this.parsedEndVal) * (progress / this.localDuration))
        } else {
          this.printVal = this.localStartVal + (this.parsedEndVal - this.localStartVal) * (progress / this.localDuration)
        }
      }
      
      if (this.localStartVal > this.parsedEndVal) {
        this.printVal = this.printVal < this.parsedEndVal ? this.parsedEndVal : this.printVal
      } else {
        this.printVal = this.printVal > this.parsedEndVal ? this.parsedEndVal : this.printVal
      }
      
      this.displayValue = this.formatValue(this.printVal)
      
      if (progress < this.localDuration) {
        this.rAF = requestAnimationFrame(this.count)
      } else {
        this.$emit('callback')
      }
    },
    
    start() {
      this.localStartVal = this.parseValue(this.startVal)
      this.startTime = null
      this.localDuration = this.duration
      this.paused = false
      this.rAF = requestAnimationFrame(this.count)
    },
    
    pause() {
      this.paused = true
      this.clearAnimationFrame()
    },
    
    resume() {
      this.paused = false
      this.startTime = null
      this.localDuration = this.remaining
      this.localStartVal = this.printVal
      this.rAF = requestAnimationFrame(this.count)
    },
    
    reset() {
      this.startTime = null
      this.localStartVal = this.parseValue(this.startVal)
      this.displayValue = this.formatValue(this.localStartVal)
      this.paused = true
      this.clearAnimationFrame()
    },
    
    startAnimation() {
      this.reset()
      this.start()
    }
  }
}
</script> 